|
1
|
|
|
/** |
|
2
|
|
|
reaction |
|
3
|
|
|
Service that processes a reaction i.e. it converts reactants to products. |
|
4
|
|
|
Many phenomenons, including decay, redox and reactions, use the react function. |
|
5
|
|
|
|
|
6
|
|
|
@namespace Services |
|
7
|
|
|
*/ |
|
8
|
|
|
'use strict'; |
|
9
|
|
|
|
|
10
|
|
|
angular |
|
11
|
|
|
.module('game') |
|
12
|
|
|
.service('reaction', ['state','util','data', |
|
13
|
|
|
function(state, util, data) { |
|
14
|
|
|
// FIXME: move to util? |
|
15
|
|
|
function isReactionCostMet (number, reaction, playerData) { |
|
16
|
|
|
for (let key in reaction.reactant) { |
|
17
|
|
|
let available = playerData.resources[key]; |
|
18
|
|
|
let required = number * reaction.reactant[key]; |
|
19
|
|
|
if (required > available) { |
|
20
|
|
|
return false; |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
return true; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/* Transforms reactants to products */ |
|
27
|
|
|
this.react = function(number, reaction, playerData) { |
|
28
|
|
|
if (!Number.isInteger(number) || number <= 0 || |
|
29
|
|
|
!reaction.reactant || !reaction.product) { |
|
30
|
|
|
return; |
|
31
|
|
|
} |
|
32
|
|
|
if (isReactionCostMet(number, reaction, playerData)) { |
|
33
|
|
|
let elements = []; |
|
34
|
|
|
for (let resource in reaction.reactant) { |
|
35
|
|
|
let required = number * reaction.reactant[resource]; |
|
36
|
|
|
playerData.resources[resource] -= required; |
|
37
|
|
|
playerData.resources[resource] = playerData.resources[resource]; |
|
38
|
|
|
// We track which elements produced the products, for the statistics |
|
39
|
|
|
for(let elem of Object.keys(data.resources[resource].elements)){ |
|
40
|
|
|
if(elements.indexOf(elem) === -1){ |
|
41
|
|
|
elements.push(elem); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
for (let resource in reaction.product) { |
|
46
|
|
|
let produced = number * reaction.product[resource]; |
|
47
|
|
|
util.addResource(playerData, elements, resource, produced, state); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
}; |
|
51
|
|
|
|
|
52
|
|
|
this.processReactions = function(reactions, player){ |
|
53
|
|
|
let declared = {}; |
|
54
|
|
|
for(let reaction of reactions){ |
|
55
|
|
|
let reactant = reaction.reaction.reactant; |
|
56
|
|
|
for (let resource in reactant) { |
|
57
|
|
|
declared[resource] = declared[resource]+reactant[resource]*reaction.number || reactant[resource]*reaction.number; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
for(let reaction of reactions){ |
|
61
|
|
|
let reactant = reaction.reaction.reactant; |
|
62
|
|
|
for (let resource in reactant) { |
|
63
|
|
|
if(!declared[resource] || !reactant[resource]){ |
|
64
|
|
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
let available = Math.min(declared[resource], player.resources[resource]); |
|
67
|
|
|
let ratio = reactant[resource]*reaction.number/declared[resource]; |
|
68
|
|
|
reaction.number = Math.min(reaction.number, Math.floor(available*ratio)); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
for(let reaction of reactions){ |
|
73
|
|
|
this.react(reaction.number, reaction.reaction, player); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
]); |
|
78
|
|
|
|